package com.gang.blog.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import com.gang.blog.dao.ArticleMapper;
import com.gang.blog.domain.Article;
import com.gang.blog.service.ArticleService;

@Service("articleServiceImpl")
public class ArticleServiceImpl implements ArticleService {
	
	@Resource
	private ArticleMapper articleMapper;
	
	public void setArticleMapper(ArticleMapper articleMapper) {
		this.articleMapper = articleMapper;
	}

	public boolean addArticle(Article article) {
		int count = this.articleMapper.insert(article);
		if(count>0){
			return true;
		}else{
			return false;
		}
	}
	/**
	 *分页查询
	 *@param pageNow 当前页面
	 *@param pageSize 页面大小  
	 */
	public List<Article> findByPage(Integer pageNow, Integer pageSize) {
		//计算出从哪一行开始，注意查询结果是从0开始的
		int pageStart = (pageNow-1)*pageSize;
		//System.out.println("pageStart="+pageStart+"limit="+ pageSize);
		List<Article> list = this.articleMapper.getArticleList(pageStart, pageSize);
		return list;
	}
	//对文章进行截取后的分页查询
	public List<Article> leftFindByPage(Integer pageNow, Integer pageSize) {
		int pageStart = (pageNow)*pageSize;
		List<Article> list = this.articleMapper.selectByleft(pageStart, pageSize);
		return list;
	}
	
	
	//返回数据库中的总记录条数
	public Integer getPageCount(Integer pageSize) {
		//得到总的文章条数
		int articleCount = this.articleMapper.getCount();
		//计算出页数
		int pageCount = (articleCount-1)/pageSize+1;
		return pageCount;
	}
	
	/**
	 * 返回文章具体信息
	 */
	public Article getArticleItem(Integer articleId) {
		
		return this.articleMapper.selectByPrimaryKey(articleId);
	}
	
	//返回文章内容在201个词之后的对象
	public Article getSubStringArticle(Integer articleId) {
		Article article = this.articleMapper.selectBySubString(articleId);
		return article;
	}
	//获取单个用户所用的文章，并且文章内容为前200个词，同时包含分页
	public List<Article> getArticleByUserId(Integer userId, Integer pageNow,
			Integer pageSize) {
		int pageStart = (pageNow)*pageSize;
		List<Article> list = this.articleMapper.selectByUserId(userId, pageStart, pageSize);
		return list;
	}
	//获取单个用户总的文章数
	public Integer getPageCountByUser(Integer pageSize) {
		Integer count = this.articleMapper.getCountByUser();
		Integer pageCount = (count-1)/pageSize+1;
		return pageCount;
	}

	
}
